package es.mllacer.qqsm;

import java.util.ArrayList;
import java.util.List;

import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import es.mllacer.qqsm.tasks.GetRemoteScoresTask;
import es.mllacer.qqsm.util.HighScore;
import es.mllacer.qqsm.util.HighScoreList;
import es.mllacer.qqsm.util.HighScoreWithAddress;
import es.mllacer.qqsm.util.LocalScoresDBHelper;
import es.mllacer.qqsm.util.RemoteScoresItemizedOverlay;

public class Scores extends MapActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.scores_layout);

		TabSpec spec = null;
		TabHost host = (TabHost) findViewById(R.id.scores_tabhost);
		host.setup();

		spec = host.newTabSpec("TAB1");
		spec.setIndicator(getResources().getString(R.string.scores_local_tab),
				null);
		spec.setContent(R.id.scrollView1);
		host.addTab(spec);

		spec = host.newTabSpec("TAB2");
		spec.setIndicator(
				getResources().getString(R.string.scores_friends_tab), null);
		spec.setContent(R.id.mapview);
		host.addTab(spec);

		host.setCurrentTabByTag("TAB1");

		// fill table with local scores
		setLocalScores();
		// draw map with remote scores
		setRemoteScores();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.scores, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		TabHost host = (TabHost) findViewById(R.id.scores_tabhost);
		// enable menu item only if TAB1 is visible
		menu.getItem(0).setEnabled("TAB1".equals(host.getCurrentTabTag()));
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.menu_scores) {
			removeLocalScores();
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	@Override
	protected boolean isLocationDisplayed() {
		return false;
	}

	private List<TableRow> localScores = new ArrayList<TableRow>();

	private TableLayout getTable() {
		return (TableLayout) findViewById(R.id.scores_local_table);
	}

	private void removeLocalScores() {
		TableLayout table = getTable();

		// delete table rows
		for (TableRow row : localScores) {
			table.removeView(row);
		}
		localScores.clear();

		// delete data in database
		LocalScoresDBHelper.getInstance(this).deleteLocalScores();
	}

	private void setLocalScores() {
		TableLayout table = getTable();
		TableRow row = null;
		TextView tv = null;

		HighScoreList scoresList = LocalScoresDBHelper.getInstance(this)
				.getLocalScores();

		for (HighScore score : scoresList.getScores()) {
			row = new TableRow(this);
			localScores.add(row);

			tv = new TextView(this);
			tv.setText(score.getName());
			row.addView(tv);

			tv = new TextView(this);
			tv.setText(((HighScoreWithAddress) score).getAddress());
			row.addView(tv);

			tv = new TextView(this);
			tv.setText(String.valueOf(score.getScoring()));
			row.addView(tv);

			table.addView(row);
		}
	}

	private void setRemoteScores() {
		MapView view = (MapView) findViewById(R.id.mapview);

		view.setBuiltInZoomControls(true);
		view.setClickable(true);

		AsyncTask<Void, Integer, Boolean> task = new GetRemoteScoresTask(this);
		task.execute();
	}

	public void setMapScores(HighScoreList list) {
		MapView view = (MapView) findViewById(R.id.mapview);

		List<Overlay> mapOverlays = view.getOverlays();
		Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);
		RemoteScoresItemizedOverlay itemizedOverlay = new RemoteScoresItemizedOverlay(
				this, drawable);

		for (HighScore hs : list.getScores()) {
			GeoPoint point = new GeoPoint((int) (hs.getLatitude() * 1e6),
					(int) (hs.getLongitude() * 1e6));
			OverlayItem item = new OverlayItem(point, hs.getName(),
					String.valueOf(hs.getScoring()));
			itemizedOverlay.addOverlay(item);
			mapOverlays.add(itemizedOverlay);
		}
	}

}
